package com.yuncheng.oaApi.controller;

import java.util.*;
import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.NkRsSrdj;
import com.yuncheng.oaApi.service.INkRsSrdjService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 内控-收入登记
 * @Author: LHB
 * @Date: 2023-11-21
 * @Version: V1.0
 */
@Api(tags = "NK_内控-收入登记接口")
@RestController
@RequestMapping("/api/spzx-oa/nkRsSrdj")
public class NkRsSrdjController extends BaseController<NkRsSrdj, INkRsSrdjService> {

    private static final Logger log = LoggerFactory.getLogger(NkRsSrdjController.class);

    @Resource
    private INkRsSrdjService nkRsSrdjService;

    /**
     * 分页列表查询
     *
     * @param nkRsSrdj
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "内控-收入登记-分页列表查询")
    @ApiOperation(value = "内控-收入登记-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkRsSrdj nkRsSrdj, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        LambdaQueryWrapper<NkRsSrdj> queryWrapper = new LambdaQueryWrapper<>();
        //登记人 登记人部门 来源 说明 金额
        queryWrapper.like(StringUtils.isNotBlank(nkRsSrdj.getFdjr()),NkRsSrdj::getFdjr,nkRsSrdj.getFdjr());
        queryWrapper.like(StringUtils.isNotBlank(nkRsSrdj.getFdjrbm()),NkRsSrdj::getFdjrbm,nkRsSrdj.getFdjrbm());
        queryWrapper.like(StringUtils.isNotBlank(nkRsSrdj.getFly()),NkRsSrdj::getFly,nkRsSrdj.getFly());
        queryWrapper.like(StringUtils.isNotBlank(nkRsSrdj.getFsm()),NkRsSrdj::getFsm,nkRsSrdj.getFsm());
        queryWrapper.like(StringUtils.isNotBlank(nkRsSrdj.getFje()),NkRsSrdj::getFje,nkRsSrdj.getFje());

        Page<NkRsSrdj> page = new Page<>(pageNo, pageSize);
        IPage<NkRsSrdj> pageList = nkRsSrdjService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    @AutoLog(value = "内控-收入登记-添加")
    @ApiOperation(value = "内控-收入登记-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkRsSrdj nkRsSrdj) {
        HttpResult<String> result = new HttpResult(true);
        CurrentUserVo user = ThreadContextHelper.getCurrentUser(); //获取当前登陆人
        if (StringUtils.isBlank(nkRsSrdj.getFdjr())){
            nkRsSrdj.setFdjr(user.getName());
        }
        if (StringUtils.isBlank(nkRsSrdj.getFdjrid())){
            nkRsSrdj.setFdjrid(user.getId());
        }
        if (StringUtils.isBlank(nkRsSrdj.getFdjrbm())){
            nkRsSrdj.setFdjrbm(user.getDeptName());
        }


        nkRsSrdjService.save(nkRsSrdj);
        result.setResult(nkRsSrdj.getId());
        result.setMessage(I18nUtil.message("save.success"));
        return result;
    }

    @AutoLog(value = "内控-收入登记-编辑")
    @ApiOperation(value = "内控-收入登记-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkRsSrdj nkRsSrdj) {
        nkRsSrdjService.updateById(nkRsSrdj);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    @AutoLog(value = "内控-收入登记-通过id删除")
    @ApiOperation(value = "内控-收入登记-通过id删除")
    @DeleteMapping(value = "/delete")
    public HttpResult<?> delete(@RequestParam("id") String id) {
        nkRsSrdjService.removeById(id);
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    @AutoLog(value = "内控-收入登记-批量删除")
    @ApiOperation(value = "内控-收入登记-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
        nkRsSrdjService.removeByIds(Arrays.asList(ids.split(",")));
        return HttpResult.ok(I18nUtil.message("delete.success"));
    }

    @AutoLog(value = "内控-收入登记-通过id查询")
    @ApiOperation(value = "内控-收入登记-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(@RequestParam("id") String id) {
        NkRsSrdj nkRsSrdj = nkRsSrdjService.getById(id);
        return HttpResult.ok(nkRsSrdj);
    }

}
